<?
include "../config.php";

// librairies
if(is_dir('library')){
	$handle=opendir('library');
	while(false!==($file=readdir($handle))){
		if(strpos($file,"lib")!==false) include('library/'.$file);
	}
}

// classes
if(is_dir('classes')){
	$handle=opendir('classes');
	while(false!==($file=readdir($handle))){
		if(strpos($file,"class")!==false) include('classes/'.$file);
	}
}

// connexion BDD
$sql=new sql();

//  recup parametrage BDD
$res=$sql->request("select * from ".CFG_MYSQL_PREFIX."config");
foreach($res as $var){
	define($var->nom,$var->valeur);
}

// Identification
include "../modules/".CFG_IDENT."/main.php";
ident_init();
if(isset($_POST["login"])&&isset($_POST["mdp"])) ident_login($_POST["login"],$_POST["mdp"]);
$GLOBALS["USER"]=unserialize($_SESSION["user"]);


$GLOBALS["CONFIG"]=array();

$res=$sql->request("select * from ".CFG_MYSQL_PREFIX."module where actif=1");
foreach($res as $var){
	$module=new module($var);
	
	/*- chargement librairies */
	if(is_dir('../modules/'.$module->nom.'/library')){
		$handle=opendir('../modules/'.$module->nom.'/library');
		while(false!==($file=readdir($handle))){
			if(strpos($file,"lib")!==false) require_once('../modules/'.$module->nom.'/library/'.$file);
		}
	}

	/*- chargement classes */
	if(is_dir('../modules/'.$module->nom.'/classes')){
		$handle=opendir('../modules/'.$module->nom.'/classes');
		while(false!==($file=readdir($handle))){
			if(strpos($file,"class")!==false) require_once('../modules/'.$module->nom.'/classes/'.$file);
		}
	}
	
	$GLOBALS["CONFIG"][$var->nom]=array();
	$res2=$sql->request("select * from ".CFG_MYSQL_PREFIX."module_config where module='".$var->nom."'");
	foreach($res2 as $var2){
		$GLOBALS["CONFIG"][$var->nom][$var2->nom]=$var2->valeur;
	}
}


if(isset($_REQUEST["page"])){
	include "../".$_REQUEST["page"];
}

